package com.hjj.services;

import com.hjj.beans.Activity;
import com.hjj.dto.ResultPageDTO;
import com.hjj.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.UUID;

@Service
public class ActivityServicesImp implements ActivityServices{
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public ArrayList<Activity> getAll(ResultPageDTO resultPageDTO) {
        // 获取总数据行数
        int count = activityMapper.getCount(resultPageDTO);

        // 将总数据行数添加到ResultPageDTO中
        resultPageDTO.setTotalRows(count);

        // 获取总页数
        int pages = count / resultPageDTO.getRowsPerPage();

        // 如果除完还有余数那么总页数就需要加1
        if (count % resultPageDTO.getRowsPerPage() != 0){
            pages += 1;
        }

        // 将中页数添加到ResultPageDTO中
        resultPageDTO.setTotalPages(pages);

        // 计算出每页的起始值
        int value = (resultPageDTO.getCurrentPage() - 1) * resultPageDTO.getRowsPerPage();

        // 将起始值添加到ResultPageDTO中
        resultPageDTO.setInitialValue(value);


        return activityMapper.getAll(resultPageDTO);
    }

    @Override
    public Activity get(String id) {
        return activityMapper.get(id);
    }

    @Override
    public Activity editBefore(String id) {
        return activityMapper.editBefore(id);
    }

    @Override
    public boolean add(Activity activity) {
        // 添加活动数据的UUID
        activity.setId(UUID.randomUUID().toString().replace("-", ""));

        // 添加创建日期时间
        activity.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return activityMapper.add(activity) > 0 ? true : false;
    }

    @Override
    public boolean edit(Activity activity) {
        // 添加创建日期时间
        activity.setEditTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return activityMapper.edit(activity) > 0 ? true : false;
    }

    @Override
    public boolean del(String[] ids) {
        return activityMapper.del(ids) > 0 ? true : false;
    }
}
